package P_2023_03_07_StringBuilder;

import java.util.Random;
import java.util.Scanner;
import java.util.StringJoiner;

public class stringBuilder {
    public static void main(String[] args) {
        //1.stringbuilder的使用
        //--字符串拼接---字符串反转
        //est1();

        //2.拼接数组
        //test2();

        //3.StringJoiner
        //test3();

        //4.罗马数字转化
        //test4();

        //5.旋转数字
        //test5();

        //6.输入一个字符串，打乱内容
        //test6();

        //7.随机生成验证码
        //test7();

        //8.两个非负字符串，求乘积（字符串）
        //test8();

        //9.给定一个字符串，输出字符串中最后一个单词的长度
        test9();

    }


    //1.判断是否是回文数
    public static void test1() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串：");
        String str = sc.next();

        String result = new StringBuilder().append(str).reverse().toString();

        if (str.equals(result)) {
            System.out.println("是回文数");
        } else {
            System.out.println("不是回文数");
        }
    }

    //2.拼接数组
    public static void test2() {
        int[] arr = new int[]{1, 2, 3, 4, 5, 6};

        String str = arrToString(arr);

        System.out.println(str);
    }

    public static String arrToString(int[] arr) {
        StringBuilder sb = new StringBuilder();
        sb.append("[");

        for (int i = 0; i < arr.length; i++) {
            if (i == arr.length - 1) {
                sb.append(arr[i]);
            } else {
                sb.append(arr[i] + ", ");
            }
        }
        sb.append("]");
        return sb.toString();
    }

    //3.stringBuilder
    public static void test3() {
        int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
        String str = arrToString2(arr);
        System.out.println(str);
    }

    public static String arrToString2(int[] arr) {
        StringJoiner sj = new StringJoiner(", ", "[", "}");
        for (int i = 0; i < arr.length; i++) {
            sj.add(arr[i] + "");
        }
        return sj.toString();
    }

    //4.罗马数字转化
    public static void test4() {
        Scanner sc = new Scanner(System.in);

        String str;
        while (true) {
            //1.输入一个字符串
            System.out.println("请输入一个字符串：");
            str = sc.next();

            //2.校验是否为九位数，且只含有数字
            boolean flag = checkStr(str);
            if (flag) {
                break;
            } else {
                System.out.println("当前输入不符合规则，请重新输入");
                continue;
            }
        }
        StringBuilder sb = new StringBuilder();
        //3.变成罗马数字
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            int number = c - 48;
            sb.append(changeRoma(number));
        }
        System.out.println(sb.toString());
    }

    public static boolean checkStr(String str) {
        if (str.length() > 9) {
            return false;
        }
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c < '0' || c > '9') {
                return false;
            }
        }
        return true;
    }

    public static String changeRoma(int num) {
        String[] arr = {"", "Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", "Ⅶ", "Ⅷ", "Ⅸ"};
        return arr[num];
    }

    //5.旋转数字
    public static void test5() {
        //1.定义两个字符串 A， B
        //旋转A, a = abc --> a = bca
        //经过n次旋转后，如果与b 相同， 则返回true

        String strA = "abcde";
        String strB = "cdeab";

        //2.调用方法进行比较
        boolean result = check(strA, strB);

        //3.输出
        System.out.println(result);
    }

    public static boolean check(String strA, String strB) {
        for (int i = 0; i < strA.length(); i++) {
            strA = rotate(strA);
            if (strA.equals(strB)) {
                return true;
            }
        }
        return false;
    }

    //旋转字符串
    public static String rotate(String str) {
        //1.用substring 截取
        /*String start = str.substring(1);
        char end = str.charAt(0);
        return start + end;*/
        //2.把字符串先变成字符数组，然后调整数据，最后再把字符数组变成字符串

        //"abc" --- ['a', 'b', 'c']     new String (字符数组)
        char[] arr = str.toCharArray();
        char first = arr[0];
        for (int i = 1; i < arr.length; i++) {
            arr[i - 1] = arr[i];
        }
        arr[arr.length - 1] = first;

        //利用字符数组创建一个字符串对象
        String result = new String(arr);
        return result;
    }

    //6.输入一个字符串，打乱内容
    public static void test6() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串：");
        String str = sc.next();

        //2.变成数组
        char[] arr = str.toCharArray();

        //3.随机打乱
        Random r = new Random();
        int rIndex = r.nextInt(arr.length);
        for (int i = 0; i < arr.length; i++) {
            char temp = arr[i];
            arr[i] = arr[rIndex];
            arr[rIndex] = temp;
        }

        //4.字符数组变成字符串
        String result = new String(arr);

        System.out.println(result);

    }

    //7.随机生成验证码
    public static void test7() {
        //1.定义字符数组  a-z , A-Z
        char[] arr = new char[]{
                'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
        };
        //2.随机生成4位字母
        Random r = new Random();
        String str = "";
        for (int i = 0; i < 4; i++) {
            int rIndex = r.nextInt(arr.length);
            str += arr[rIndex];
        }

        //3.生成 0-9 数字，并拼接
        str += r.nextInt(10);
        System.out.println(str);

        char[] arrStr = str.toCharArray();
        int rNum = r.nextInt(4);
        char temp = arrStr[rNum];
        arrStr[rNum] = arrStr[4];
        arrStr[4] = temp;

        String result = new String(arrStr);
        System.out.println(result);
    }

    //8.两个非负字符串，求乘积（字符串）
    public static void test8() {
        String str1 = "32";
        String str2 = "32";

        int num1 = changeNum(str1);
        int num2 = changeNum(str2);

        System.out.println(num1);
        System.out.println(num2);

        int sum = num1 * num2;
        String result = sum + "";
        System.out.println("结果为：" + result);

    }

    public static int changeNum(String str) {
        int num = 0;
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            int ge = c - 48;
            num = ge + num * 10;
        }
        return num;
    }

    //9.给定一个字符串，输出字符串中最后一个单词的长度
    public static void test9() {
        //用计数器，倒着遍历字符串，出现空格时停止计数
        String str = "you are my best friend";

        int count = 0;
        for (int i = str.length() - 1; i >= 0; i--) {
            char c = str.charAt(i);
            if (c != ' ') {
                count++;
            } else {
                break;
            }
        }
        System.out.println(count);
    }

}